//index.js
//获取应用实例
const event = require('../../tqlsdk/event')
const app = getApp()
const tqlSDK = app.globalData.tqlSDK
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({
  data: {
    showDialog: false,
    dialogMessage: '在使用本小程序前请打开您的手机蓝牙',
    isSearching: false,
    blueToothOn: false,
    blueToothList: null,
    connectDevice: null,
    showConnect: false
  },
  onLoad: function () {
    this.initBluetooth();
    event.on('unConnect', this, (data) => {
      this.setData({
        showConnect: false,
        connectDevice: null
      })
    })

    // event.on('disconnect',this,(data)=>{
    //   this.setData({
    //     showConnect:data
    //   })
    // })
  },
  onClose() {
    this.setData({
      showDialog: false
    })
  },
  search() {
    this.initBluetooth()
    if (!this.data.blueToothOn) {
      return
    }
    if (this.data.isSearching) {
      this.setData({
        isSearching: false,
        isLoading: true,
        clickDisable: true,
        deviceId: null
      })
      return
    }
    this.setData({
      isSearching: true,
      isLoading: true,
      clickDisable: true
    })
    this.searchBlueTooth();
  },
  // 初始化蓝牙
  initBluetooth() {
    tqlSDK.initBlueTooth(() => {
      this.setData({
        blueToothOn: true
      })
    }, () => {
      this.setData({
        showDialog: true
      })
    })
  },
  // 搜索附近的蓝牙设施
  searchBlueTooth() {
    tqlSDK.searchBlueTooth(this.blueToothList)
    setTimeout(() => {
      this.setData({
        isLoading: false,
        clickDisable: false
      })
    }, 2000)
  },
  // 查找到的设备列表
  blueToothList() {
    tqlSDK.showBlueToothList(this)
  },
  // 连接蓝牙
  connectBlueTooth(e) {
    this.setData({
      deviceId: e.currentTarget.dataset.deviceid
    })
    Toast.loading({
      message: '正在连接',
      duration: 0,
      forbidClick: true
    })
    tqlSDK.createBlueToothConnection(e.currentTarget.dataset.deviceid, () => {
      this.getBlueToothService()
    }, () => {
      Toast.clear()
    })
  },
  // 获取蓝牙设备服务
  getBlueToothService() {
    // 监听蓝牙连接断开
    tqlSDK.listenBlueConnection()
    // 获取蓝牙特征值
    tqlSDK.getBlueToothService(this.data.deviceId, () => {
      this.data.blueToothList.foundDevices.forEach(item => {
        if (item.deviceId === this.data.deviceId) {
          this.setData({
            connectDevice: item,
            showConnect: true
          })
        }
      })
      Toast.clear();
      Toast.success({
        message: '连接成功',
        duration: 1000,
        onClose: () => {
          wx.navigateTo({
            url: '../writePage/writePage',
          })
        }
      })
    })
  },
  // 刷新列表
  refreshList() {
    Toast.loading({
      message: '刷新中...',
      forbidClick: true,
      duration: 1500,
      onClose: () => {
        setTimeout(() => {
          Toast.success({
            duration: 500,
            message: '刷新成功',
            onClose: () => {
              tqlSDK.allDevices.foundDevices = [];
              this.searchBlueTooth();
            }
          })
        }, 100)
      }
    })
  },
  // 去书写页面
  gotoWrite() {
    wx.navigateTo({
      url: '../writePage/writePage',
    })
  },
  // 去demo
  goDemo() {
    wx.navigateTo({
      url: '../demo/demo',
    })
  },
  // 断开连接
  disConnect() {
    Toast.loading({
      message: '正在断开连接',
      forbidClick: true,
      duration: 2000,
      onClose: () => {
        tqlSDK.unConnectBlueTooth(this.data.connectDevice.deviceId, () => {
          this.setData({
            showConnect: false,
            connectDevice: null
          })
          Toast.success({
            message: '断开成功',
            duration: 500
          })
        })
      }
    })
  },
  onShow: function () {
    wx.setKeepScreenOn({
      keepScreenOn: true
    })
  }
})